package com.yongcheon.sms_manager.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * Created by yongcheon on 2014-11-20.
 */
public class StringUtil {

    public static String changeUperCaseAt(String str, int index) {
        String returnStr = "";
        String beferStr = str.substring(0, index - 1);
        String changeStr = StringUtils.upperCase(str.substring(index - 1, index));
        String afterStr = str.substring(index, str.length());
        returnStr = beferStr + changeStr + afterStr;
        return returnStr;
    }

    public static String changeSpecialLettersForSQL(String string){
        String returnStr = null;
        if(string != null) returnStr = string.replaceAll("'","");
        return returnStr;
    }

    public static int getIndexInStringArray(String[] array, String str){
        int returnInt = -1;
        for(int i=0; i<array.length; i++){
            if(array[i].equals(str)){
                returnInt = i;
            }
        }
        return returnInt;
    }

    public static String replaceFormFeeds(String s) {
        return s == null ? "" : s.replace('\f', '\n');
    }

    public static boolean isLetter(String str){
        boolean returnBool = false;
        if(Pattern.matches("[a-z]", str)){
            returnBool = true;
        }else if(Pattern.matches("[A-Z]", str)){
            returnBool = true;
        }else if(Pattern.matches("[0-9]", str)){
            returnBool = true;
        }else if(Pattern.matches("[가-힝]", str)){
            returnBool = true;
        }
        return returnBool;
    }

    public static String getOnlyLetter(String str){
        String returnStr = "";
        StringBuffer buffer = new StringBuffer();
        for(int i=0; i<str.length(); i++){
            String tmp = str.substring(i, i+1);
            if(isLetter(tmp)) buffer.append(tmp);
        }
        returnStr = buffer.toString();
        return returnStr;
    }
}
